package com.prs.crm.action.sys;


import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.sys.CodeCommon;
import com.prs.crm.service.sys.CodeCommonService;


public class CodeCommonAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	
	private CodeCommon codeCommon;
	private CodeCommonService  codeCommonService;

	private PaginationSupport pagination;
	private Integer[] codeCommonIds;
	
	private  String encodes;
	
	
	@Action("editCodeCommon")
	public String editCodeCommon(){	
		
		return SUCCESS;
	}
	
	@Action(value="saveCodeCommon",results={@Result(name = "error", type = "chain", location = "editCodeCommon"),@Result(name="success",type="redirect",location="listCodeCommon")})
	public String saveCodeCommon()
	{
		if(codeCommon.getCodeNumber()==null||codeCommon.getCodeNumber().equals("")){
			this.addActionError("流水号位数 不能为空");
			return ERROR;
		}
		if(codeCommon.getSerialcode()==null||codeCommon.getSerialcode().equals("")){
			this.addActionError("起始流水号不能为空");
			return ERROR;
		}
		if(codeCommon.getCodePlan()==null||codeCommon.getCodePlan().equals("")){
			this.addActionError("方案名称不能为空");
			return ERROR;
		}
		int a=0;
		try {
			a=Integer.parseInt(codeCommon.getCodeNumber());
		} catch (Exception e) {
			this.addActionError("流水号位数 只能为数字");
			return ERROR;
		}
		try {
			Integer.parseInt(codeCommon.getSerialcode());
		} catch (Exception e) {
			this.addActionError("起始流水号 只能为数字");
			return ERROR;
		}
		if(codeCommon.getSerialcode().length()>a){
			this.addActionError("起始流水号 的位数大于流水号位数");
			return ERROR;
		}
		
		/**
		 * 是否默认
		 */
		
		if ("是".equals(codeCommon.getDefault1())) {
			codeCommon.setPrefix(codeCommon.getPrefix());
		} else if ("否".equals(codeCommon.getDefault1())){
			
			codeCommon.setPrefix("");
		}
		
		/**
		 * 起始流水号
		 */

		  int digitValue=Integer.parseInt(codeCommon.getCodeNumber());
		  String serialcode=codeCommon.getSerialcode();
		if (digitValue!= serialcode.length()) {
			String OneNum = appendEke(digitValue - serialcode.length());
			codeCommon.setSerialcode(OneNum + serialcode);
			codeCommon.setNumBackUp(OneNum + serialcode);
		} else {
			codeCommon.setSerialcode(serialcode);
			codeCommon.setNumBackUp(serialcode);
		}
	
	   this.getCodeCommonService().save(codeCommon);
	   
		return SUCCESS;
	}
	
	@Action(value = "CEncodeing", results = { @Result(name = "success", type = "json", params = {
			"root", "encodes" }) })			
	public String CEncodeing() {
		CodeCommon VObject=this.getCodeCommonService().get(codeCommon.getId());			
		encodes=EnCoding(VObject);	
		return SUCCESS;
	}
	
	@Action("listCodeCommon")
	public String listCodeCommon(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(CodeCommon.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCodeCommonService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action(value="removeCodeCommon",results={@Result(name="success",type="redirect",location="listCodeCommon")})
	public String removeCodeCommon(){
		int length=codeCommonIds.length;
		for(int i=0;i<length;i++){
			CodeCommon code=getCodeCommonService().getVobject(codeCommonIds[i]);
			this.getCodeCommonService().delete(code);
		}
		return SUCCESS;
	}
	
	
	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	
	
	public CodeCommon getCodeCommon() {
		return codeCommon;
	}

	public void setCodeCommon(CodeCommon codeCommon) {
		this.codeCommon = codeCommon;
	}

	public CodeCommonService getCodeCommonService() {
		return codeCommonService;
	}

	public void setCodeCommonService(CodeCommonService codeCommonService) {
		this.codeCommonService = codeCommonService;
	}

	public Integer[] getCodeCommonIds() {
		return codeCommonIds;
	}

	public void setCodeCommonIds(Integer[] codeCommonIds) {
		this.codeCommonIds = codeCommonIds;
	}
	
	private static String appendEke(int num) {
		String returnV = "";
		for (int i = 0; i < num; i++) {
			returnV = returnV + "0";
		}
		return returnV;
	}
	
	@Action(value = "Encodeing", results = { @Result(name = "success", type = "json", params = {
			"root", "encodes" }) })
	private static String EnCoding(CodeCommon codeCommon){
		String Encodings=codeCommon.getPrefix()+codeCommon.getNumBackUp();
		int numbackUp=Integer.parseInt(codeCommon.getNumBackUp());
		int codeNumber=Integer.parseInt(codeCommon.getCodeNumber());
		String a="";
		for(int i=0;i<codeNumber;i++){
			a=a+"9";
		}
		int num=Integer.parseInt(a);
		if(num<=numbackUp){
			Encodings=Encodings+",1";
		}else{
			Encodings=Encodings+",0";
		}
		return Encodings.trim();
	}
	
	
	public String getEncodes() {
		return encodes;
	}

	public void setEncodes(String encodes) {
		this.encodes = encodes;
	}

}
